﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Reflection;
using System.Web.Mvc;

namespace MvcMovie
{
    public class IsRouteActionConstraint : IRouteConstraint
    {
        private Dictionary<string, Type> mControllers;

        public IsRouteActionConstraint()
        {
            mControllers = Assembly
                .GetCallingAssembly()
                .GetTypes()
                .Where(type => type.IsSubclassOf(typeof(Controller)))
                .ToDictionary(key => key.Name.Replace("Controller", ""));
        }

        public bool Match(
            HttpContextBase httpContext, 
            Route route, 
            string parameterName, 
            RouteValueDictionary values, 
            RouteDirection routeDirection)
        {
            return !mControllers.Keys.Contains(values["action"] as string);
        }
    }
}